﻿using AutoMapper;
using Modules;
using Modules.Attachment;
using Modules.Common;
using Modules.Project;
using Pipelines.Sockets.Unofficial.Arenas;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using System.ComponentModel.DataAnnotations;
using Modules.System.User;

namespace ManagementApi.Models.Project;

/// <summary>
/// 完工销户申请Dto
/// </summary>
public class ProjectCompletedDto : OperateDto
{
    /// <summary>
    /// 主键
    ///</summary>
    public int Id { get; set; }

    /// <summary>
    /// 项目ID
    ///</summary>
    public int? ProjectId { get; set; }

    /// <summary>
    /// 申请理由
    ///</summary>
    public string? ApplyReason { get; set; }

    public int AccountId { get; set; }

    /// <summary>
    /// 申请状态
    ///</summary>
    public ProjectConstants.完工销户申请状态? AuditType { get; set; }

    /// <summary>
    /// 申请状态
    ///</summary>
    public string? AuditTypeName { get; set; }

    /// <summary>
    /// 实际竣工日期
    ///</summary>
    public DateTime? EndDate { get; set; }

    /// <summary>
    /// 退还保证金
    ///</summary>
    public decimal? RefundAmount { get; set; }

    /// <summary>
    /// 可退还金额
    ///</summary>
    public decimal? RefundableAmount { get; set; }

    /// <summary>
    /// 审核/退还原因说明
    ///</summary>
    public string? RefundReason { get; set; }

    /// <summary>
    /// 申请时间
    /// </summary>
    public DateTime? CreateTime { get; set; }

    /**********额外字段*************/

    /// <summary>
    /// 项目名称
    /// </summary>
    public string? ProjectName { get; set; }

    /// <summary>
    /// 项目编号
    /// </summary>
    public string? ProjectNum { get; set; }

    /// <summary>
    /// 项目类型
    ///</summary>
    public string? CategoryName { get; set; }

    /// <summary>
    /// 建设性质
    ///</summary>
    public string? PropertyNumName { get; set; }

    /// <summary>
    /// 计划开工日期
    ///</summary>
    public DateTime? ContractStartDate { get; set; }

    /// <summary>
    /// 计划竣工日期
    ///</summary>
    public DateTime? ContractEndDate { get; set; }

    /// <summary>
    /// 负责人
    /// </summary>
    public string? LinkMan { get; set; }

    /// <summary>
    /// 负责人电话
    /// </summary>
    public string? LinkPhone { get; set; }

    /// <summary>
    /// 附件集合
    /// </summary>

    public Dictionary<附件类型, List<AttachmentsForm<int>>> dicAttachments { get; set; } = new Dictionary<附件类型, List<AttachmentsForm<int>>>();
}

/// <summary>
/// 完工销户申请扩展
/// </summary>
public static class ProjectCompletedExceptions
{
    /// <summary>
    /// 返回前端显示数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static ProjectCompletedDto AsDto(this ProjectCompleted entity, IApplicationContext applicationContext)
    {
        ProjectCompletedDto dto = new();
        applicationContext.GetService<IMapper>().Map(entity, dto);
        dto.AuditTypeName = dto.AuditType?.GetDescription();
        var projectService = applicationContext.GetService<ProjectService>();
        var project = projectService.GetProjectInfo(entity.ProjectId);
        if (project != null)
        {
            dto.ProjectName = project.Name;
            dto.ProjectNum = project.PrjNum;
            dto.ContractStartDate = project.ContractStartDate;
            dto.ContractEndDate = project.ContractEndDate;
            dto.LinkMan = project.LinkMan;
            dto.LinkPhone = project.LinkPhone;
            dto.CategoryName = project.Category?.GetDescription();
            dto.PropertyNumName = project.PropertyNum?.GetDescription();

            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator() || project.DeptId == deptUser.DeptId)
                    dto.HasEdit = true;
                else dto.HasEdit = false;
            }

            #endregion 主管单位数据权限
        }

        #region 附件

        var projectAttachmentService = applicationContext.GetService<ProjectAttachmentService>();
        var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType(dto.Id, 项目附件租户.ProjectCompleted);
        var projectAttachments = projectAttachmentService.GetProjectAttachmentsByObjectIdAndType(dto.Id, 项目附件租户.ProjectCompleted);
        Dictionary<附件类型, List<AttachmentsForm<int>>> dicAttachments = new();
        foreach (var item in projectAttachments)
        {
            var attachment = attachments.FirstOrDefault(i => i.Id == item.AttachmentId);
            if (attachment == null) continue;
            if (dicAttachments.ContainsKey(item.AttachmentType))
            {
                dicAttachments[item.AttachmentType].Add(new AttachmentsForm<int>()
                {
                    AttachmentId = attachment.Id,
                    AttachmentName = attachment.FriendlyName,
                });
            }
            else
            {
                dicAttachments[item.AttachmentType] = new List<AttachmentsForm<int>>() {
                        new AttachmentsForm<int>() {
                            AttachmentId = attachment.Id,
                            AttachmentName = attachment.FriendlyName,
                        }
                };
            }
        }
        dto.dicAttachments = dicAttachments;

        #endregion 附件

        return dto;
    }
}